home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Applications / Workspace / TickleServices / Documentation / E.Aux / service.m < prev    next >
Encoding:
Text File  |  1994-03-22  |  1.8 KB  |  63 lines

  1. /* A variation on a command-line program to run a service.
  2.  * Use it as you see fit.
  3.  * Scott Hess <scott@nic.gac.edu>
  4.  *
  5.  * cc -o service -s -O service.m -lNeXT_s
  6.  * cc -arch m68k -arch i386 -o service -s -O service.m -lNeXT_s
  7.  */
  8. #import <appkit/appkit.h>
  9.  
  10. NXDefaultsVector defs={
  11.     { "Input", "NeXT plain ascii pasteboard type"},
  12.     { "Output", "NeXT plain ascii pasteboard type"},
  13.     { "Service", NULL},
  14.     { NULL, NULL},
  15. };
  16. void main( int argc, char **argv)
  17. {
  18.     Pasteboard *pb=[Pasteboard newUnique];
  19.     const char *inType=NULL, *outType=NULL, *service=NULL;
  20.     NXApp=[Application new];
  21.     NXRegisterDefaults( "service", defs);
  22.     inType=NXGetDefaultValue( "service", "Input");
  23.     outType=NXGetDefaultValue( "service", "Output");
  24.     service=NXGetDefaultValue( "service", "Service");
  25.     if( inType && *inType) {
  26.     NXStream *stream=NXOpenMemory( NULL, 0, NX_WRITEONLY);
  27.     char *data;
  28.     int length, maxLength;
  29.     #define BUFSIZE 4096
  30.     char buf[ BUFSIZE];
  31.     while( (length=fread( buf, BUFSIZE, 1, stdin))>0) {
  32.         NXWrite( stream, buf, length);
  33.     }
  34.     [pb declareTypes:&inType num:1 owner:nil];
  35.     NXGetMemoryBuffer( stream, &data, &length, &maxLength);
  36.     [pb writeType:inType data:data length:length];
  37.     NXCloseMemory( stream, NX_FREEBUFFER);
  38.     }
  39.     if( NXPerformService( service, pb) && outType && *outType) {
  40.         const char *const *types=[pb types];
  41.     char *data;
  42.     int length;
  43.     while( *types && strcmp( outType, *types)) {
  44.         types++;
  45.     }
  46.     if( *types) {
  47.         [pb readType:*types data:&data length:&length];
  48.         fwrite( data, length, 1, stdout);
  49.         [pb deallocatePasteboardData:data length:length];
  50.     } else {
  51.         pb=[pb free];
  52.         exit( 1);
  53.     }
  54.     }
  55.     pb=[pb free];
  56. }
  57. /*
  58. Allow for a list of input types.
  59. Allow for a list of output types.
  60. Allow to specify file descriptors for input and output types.
  61. Allow for an input source default.
  62. */
  63.